package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.entity.Category;
import cn.tedu.coolshark.mapper.CategoryMapper;
import cn.tedu.coolshark.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

/**
 * @Classname: CategoryController
 * @Author: bromide
 * @CreateTime: 2022/5/17--14:14
 * @Version: V1.0
 * @Description:
 */
@RestController
public class CategoryController {
    //@Value 注解这种写法是找到配置文件中的值读取出来并赋值给dirPath变量
    @Value("${dirPath}")
    private String dirPath;

    @Autowired(required = false)
    CategoryMapper mapper;
    @Autowired(required = false)
    ProductMapper productMapper;

    @RequestMapping("category/insert")
    public void insert(Category category){
        mapper.insert(category);
    }

    @RequestMapping("/category/select")
    public List<Category> select(){
        return mapper.select();
    }

    @RequestMapping("/category/delete")
    public void delete(Category category){
        mapper.delete(category);
        //删除和该分类相关的商品
        //删除图片
        List<String> list = productMapper.selectUrlByCid(category.getId());
        for(String s : list){
            String filePath = dirPath+"/"+s;
            new File(filePath).delete();
        }
        productMapper.deleteByCid(category.getId());
    }
}